#ifndef BMC_TLNETDB_H_
#define BMC_TLNETDB_H_

#include <tlpr/tltypes.h>
#include <tlpr/tllong.h>
#include <tlpr/platform_library_speific.h>

TL_BEGIN_EXTERN_C
/***********************************************************************
** FUNCTIONS: TL_ntohs, TL_ntohl, TL_ntohll, TL_htons, TL_htonl, TL_htonll
**
** DESCRIPTION: API entries for the common byte ordering routines.
**
**      TL_ntohs        16 bit conversion from network to host
**      TL_ntohl        32 bit conversion from network to host
**      TL_ntohll       64 bit conversion from network to host
**      TL_htons        16 bit conversion from host to network
**      TL_htonl        32 bit conversion from host to network
**      TL_ntonll       64 bit conversion from host to network
**
***********************************************************************/
TL_INLINE_SPECIFIER TLUint16 TL_ntohs(TLUint16 n) { return ntohs(n); }
TL_INLINE_SPECIFIER TLUint32 TL_ntohl(TLUint32 n) { return ntohl(n); }
TL_INLINE_SPECIFIER TLUint16 TL_htons(TLUint16 n) { return htons(n); }
TL_INLINE_SPECIFIER TLUint32 TL_htonl(TLUint32 n) { return htonl(n); }
TL_INLINE_SPECIFIER TLUint64 TL_ntohll(TLUint64 n)
{
#ifdef IS_BIG_ENDIAN
    return n;
#else
    TLUint64 tmp;
    TLUint32 hi, lo;
    LL_L2UI(lo, n);
    LL_SHR(tmp, n, 32);
    LL_L2UI(hi, tmp);
    hi = TL_ntohl(hi);
    lo = TL_ntohl(lo);
    LL_UI2L(n, lo);
    LL_SHL(n, n, 32);
    LL_UI2L(tmp, hi);
    LL_ADD(n, n, tmp);
    return n;
#endif
}  /* ntohll */

TL_INLINE_SPECIFIER TLUint64 TL_htonll(TLUint64 n)
{
#ifdef IS_BIG_ENDIAN
    return n;
#else
    TLUint64 tmp;
    TLUint32 hi, lo;
    LL_L2UI(lo, n);
    LL_SHR(tmp, n, 32);
    LL_L2UI(hi, tmp);
    hi = htonl(hi);
    lo = htonl(lo);
    LL_UI2L(n, lo);
    LL_SHL(n, n, 32);
    LL_UI2L(tmp, hi);
    LL_ADD(n, n, tmp);
    return n;
#endif
}  /* htonll */

TL_END_EXTERN_C

#endif //BMC_TLNETDB_H_
